Scroll Progress Bar

Math Library Functions

The math library provides a set of mathematical functions that allow performing various mathematical calculations. To use these functions, need to include the math.h header.

Here are some commonly used math library functions along with a sample program demonstrating their usage:
  • sqrt(): Calculates the square root of a number.
  • pow(): Raises a number to a given power.
  • sin(): Calculates the sine of an angle (in radians).
  • cos(): Calculates the cosine of an angle (in radians).
  • tan(): Calculates the tangent of an angle (in radians).
  • exp(): Calculates the exponential value of a number (e^x).
  • log(): Calculates the natural logarithm (base e) of a number.
  • log10(): Calculates the base 10 logarithm of a number.
Sample program that uses these functions:

#include <stdio.h>
#include <math.h>

int main() {
    double num = 16.0;
    double angle = 30.0;

    // Calculate the square root of num
    double squareRoot = sqrt(num);
    printf("Square root of %.2f: %.2f\n", num, squareRoot);

    // Calculate the value of num raised to the power of 3
    double power = pow(num, 3);
    printf("%.2f raised to the power of 3: %.2f\n", num, power);

    // Convert the angle from degrees to radians
    double radians = angle * M_PI / 180.0;

    // Calculate the sine, cosine, and tangent of the angle in radians
    double sineValue = sin(radians);
    double cosineValue = cos(radians);
    double tangentValue = tan(radians);

    printf("Angle: %.2f degrees\n", angle);
    printf("Sine: %.2f\n", sineValue);
    printf("Cosine: %.2f\n", cosineValue);
    printf("Tangent: %.2f\n", tangentValue);

    // Calculate the exponential value of num
    double expValue = exp(num);
    printf("Exponential value of %.2f: %.2f\n", num, expValue);

    // Calculate the natural logarithm of num
    double logValue = log(num);
    printf("Natural logarithm of %.2f: %.2f\n", num, logValue);

    // Calculate the base 10 logarithm of num
    double log10Value = log10(num);
    printf("Base 10 logarithm of %.2f: %.2f\n", num, log10Value);

    return 0;
}
Explanation:

In this program, calculate various mathematical operations using the functions from the math library.

  • Calculate the square root of num using sqrt() and store it in the variable squareRoot.
  • Calculate the value of num raised to the power of 3 using pow() and store it in the variable power.
  • convert the angle from degrees to radians using the conversion factor M_PI (defined in math.h) and then calculate the sine, cosine, and tangent of the angle in radians using the sin(), cos(), and tan() functions, respectively.
  • Calculate the exponential value of num using exp() and store it in the variable expValue.
  • Calculate the natural logarithm (base e) of num using log() and store it in the variable logValue.
  • Calculate the base 10 logarithm of num using log10() and store it in the variable log10Value.
Sample Output:

Square root of 16.00: 4.00
16.00 raised to the power of 3: 4096.00
Angle: 30.00 degrees
Sine: 0.50
Cosine: 0.87
Tangent: 0.58
Exponential value of 16.00: 8886110.52
Natural logarithm of 16.00: 2.77
Base 10 logarithm of 16.00: 1.20

In the output, the results of the mathematical calculations performed using the math library functions.


What library is used for math functions in C?


math.h

What function rounds a floating-point number to the nearest integer in C?


round

What function calculates the absolute value of a number in C?


fabs

What function calculates the square root of a number in C?


sqrt

What function returns the smallest integer greater than or equal to a number in C?


ceil